library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity keyHandler is
  port(clk			: in std_logic;
		 KC_current : in std_logic_vector(7 downto 0);
		 KC_last 	: in std_logic_vector(7 downto 0);
		 KC_third	: in std_logic_vector(7 downto 0);
       out_up 		: out std_logic := '0';
       out_down 	: out std_logic := '0';
       out_left 	: out std_logic := '0';
       out_right 	: out std_logic := '0'
		 );
end keyHandler;

architecture onEvent of keyHandler is

begin
	process (clk)
	begin
			if(rising_edge(clk))
			then
			
				--KEY:A
--				if (keyCode = "00011100") --1C
--				then
--					if (keyCode = "11110000") --F0
--					then
--						up <= '0';
--					else
--						up <= '1';
--					end if;
--				end if;
				--END KEY:A
				
				--KEY:UP ARROW
				if (KC_current = X"75")
				then
					if (KC_last = X"E0")
					then
						out_up <= '1';
					elsif (KC_last = X"F0")
					then
						if (KC_third = X"E0")
						then
							out_up <= '0';
						end if;
					end if;
				end if;
				--END KEY:UP ARROW
				
				--KEY:DOWN DOWN
				if (KC_current = X"72")
				then
					if (KC_last = X"E0")
					then
						out_down <= '1';
					elsif (KC_last = X"F0")
					then
						if (KC_third = X"E0")
						then
							out_down <= '0';
						end if;
					end if;
				end if;
				--END KEY:DOWN ARROW
				
				--KEY:LEFT ARROW
				if (KC_current = X"6B")
				then
					if (KC_last = X"E0")
					then
						out_left <= '1';
					elsif (KC_last = X"F0")
					then
						if (KC_third = X"E0")
						then
							out_left <= '0';
						end if;
					end if;
				end if;
				--END KEY:LEFT ARROW
				
				--KEY:RIGHT ARROW
				if (KC_current = X"74")
				then
					if (KC_last = X"E0")
					then
						out_right <= '1';
					elsif (KC_last = X"F0")
					then
						if (KC_third = X"E0")
						then
							out_right <= '0';
						end if;
					end if;
				end if;
				--END KEY:RIGHT ARROW
				
			end if;
			--lastEvent <= keyCode;
	end process;

end architecture onEvent;